package com.example.myshop.service.Imp;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.myshop.domain.OrderDao;
import com.example.myshop.domain.OrderItemDao;
import com.example.myshop.entity.Address;
import com.example.myshop.entity.Order;
import com.example.myshop.entity.OrderItem;
import com.example.myshop.service.AddressService;
import com.example.myshop.service.CartService;
import com.example.myshop.service.OrderService;
import com.example.myshop.service.ex.InsertException;
import com.example.myshop.vo.CartVO;
import com.example.myshop.vo.OrderVO;
import com.example.myshop.vo.PayVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Calendar;
import java.util.Date;
import java.util.List;

@Service
public class OrderServiceImp extends ServiceImpl<OrderDao, Order> implements OrderService {
    @Autowired
    private OrderDao orderDao;
    @Autowired
    private OrderItemDao orderItemDao;
    @Autowired
    private AddressService addressService;
    @Autowired
    private CartService cartService;

    @Override
    public Order create(Long aid, Long[] cids, Long uid, String username) {
// 根据cids查询所勾选的购物车列表中的数据 即将下单的列表
        List<CartVO> carts = cartService.getVOByCid(uid, cids);
        // 计算所有商品的总价
        long totalPrice = 0L;
        for (CartVO cart : carts) {
            totalPrice += cart.getRealPrice() * cart.getNum();
        }
        // 查询收货地址数据
        Address address = addressService.getByAid(aid, uid);

        Order order = new Order();
        order.setUid(uid);
        order.setRecvName(address.getName());
        order.setRecvPhone(address.getPhone());
        order.setRecvProvince(address.getProvinceName());
        order.setRecvCity(address.getCityName());
        order.setRecvArea(address.getAreaName());
        order.setRecvAddress(address.getAddress());
        //是否支付 总价
        order.setStatus(0);
        order.setTotalPrice(totalPrice);
        // 补全数据：下单时间
        order.setOrderTime(new Date());
        // 补全数据：日志
        order.setCreatedUser(username);
        order.setCreatedTime(new Date());
        order.setModifiedUser(username);
        Date date=new Date();
        order.setModifiedTime(date);
        Integer rows= orderDao.insert(order);
        if(rows!=1){
            throw new InsertException("插入数据异常");
        }
        Calendar calendar = Calendar.getInstance();
       // calendar.set(Calendar.HOUR_OF_DAY, 0);
       // calendar.set(Calendar.MINUTE, 0);
       // calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);
        date = calendar.getTime();
        QueryWrapper<Order>queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("modified_time",date);
        queryWrapper.select("oid");
        Long oid=orderDao.selectOne(queryWrapper).getOid();
        //创建订单详细数据项 遍历carts，循环插入订单商品数据
        for(CartVO cart:carts){
            //创建订单详细数据项 订单商品数据
            OrderItem orderItem=new OrderItem();
            orderItem.setOid(oid);
            // 补全数据：pid, title, image, price, num
            orderItem.setPid(cart.getPid());
            orderItem.setTitle(cart.getTitle());
            orderItem.setImage(cart.getImage());
            orderItem.setPrice(cart.getRealPrice());
            orderItem.setNum(cart.getNum());
            // 补全数据：4项日志
            orderItem.setCreatedUser(username);
            orderItem.setCreatedTime(new Date());
            orderItem.setModifiedUser(username);
            orderItem.setModifiedTime(new Date());
            //插入数据操作
            Integer  rows2= orderItemDao.insert(orderItem);
            if(rows2!=1){
                throw new InsertException("插入订单商品数据时出现未知错误，请联系系统管理员");
            }
        }
        order.setOid(oid);
        return order;
    }

    @Override
    public Order create1(Long aid, Long[] cids, Long uid, String username) {
        // 根据cids查询所勾选的购物车列表中的数据 即将下单的列表
        List<CartVO> carts = cartService.getVOByCid(uid, cids);
        // 计算所有商品的总价
        long totalPrice = 0L;
        for (CartVO cart : carts) {
            totalPrice += cart.getRealPrice() * cart.getNum();
        }
        // 查询收货地址数据
        Address address = addressService.getByAid(aid, uid);

        Order order = new Order();
        order.setUid(uid);
        order.setRecvName(address.getName());
        order.setRecvPhone(address.getPhone());
        order.setRecvProvince(address.getProvinceName());
        order.setRecvCity(address.getCityName());
        order.setRecvArea(address.getAreaName());
        order.setRecvAddress(address.getAddress());
        //是否支付 总价
        order.setStatus(1);
        order.setTotalPrice(totalPrice);
        // 补全数据：下单时间
        order.setOrderTime(new Date());
        order.setPayTime(new Date());
        // 补全数据：日志
        order.setCreatedUser(username);
        order.setCreatedTime(new Date());
        order.setModifiedUser(username);
        Date date=new Date();
        order.setModifiedTime(date);

        Integer rows= orderDao.insert(order);
        if(rows!=1){
            throw new InsertException("插入数据异常");
        }
        Calendar calendar = Calendar.getInstance();
        // calendar.set(Calendar.HOUR_OF_DAY, 0);
        // calendar.set(Calendar.MINUTE, 0);
        // calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);
        date = calendar.getTime();
        QueryWrapper<Order>queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("modified_time",date);
        queryWrapper.select("oid");
        Long oid=orderDao.selectOne(queryWrapper).getOid();
        //创建订单详细数据项 遍历carts，循环插入订单商品数据
        for(CartVO cart:carts){
            //创建订单详细数据项 订单商品数据
            OrderItem orderItem=new OrderItem();
            orderItem.setOid(oid);
            // 补全数据：pid, title, image, price, num
            orderItem.setPid(cart.getPid());
            orderItem.setTitle(cart.getTitle());
            orderItem.setImage(cart.getImage());
            orderItem.setPrice(cart.getRealPrice());
            orderItem.setNum(cart.getNum());
            // 补全数据：4项日志
            orderItem.setCreatedUser(username);
            orderItem.setCreatedTime(new Date());
            orderItem.setModifiedUser(username);
            orderItem.setModifiedTime(new Date());
            //插入数据操作
            Integer  rows2= orderItemDao.insert(orderItem);
            if(rows2!=1){
                throw new InsertException("插入订单商品数据时出现未知错误，请联系系统管理员");
            }
        }
        order.setOid(oid);
        return order;
    }

    @Override
    public List<OrderVO> findVOByOid(Long oid) {
        List<OrderVO> list=orderDao.findVOByOid(oid);
        return list;
    }

    @Override
    public Order findOrderByOid(Long oid) {
        QueryWrapper<Order>queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("oid",oid);
        Order order=orderDao.selectOne(queryWrapper);
        return order;
    }

    @Override
    public void updateStatus(Long oid, Date payTime) {
        UpdateWrapper<Order>updateWrapper=new UpdateWrapper<>();
        updateWrapper.eq("oid",oid);
        updateWrapper.set("pay_time",new Date());
        updateWrapper.set("status",1);
        orderDao.update(null,updateWrapper);
    }

    @Override
    public List<OrderVO> findVOByUid(Long uid) {
        List<OrderVO> list=orderDao.findVOByUid(uid);
        return list;
    }

    @Override
    public List<OrderVO> findBVOByUid(Long uid) {
        List<OrderVO> list=orderDao.findBVOByUid(uid);

        return list;
    }

    @Override
    public List<OrderVO> findVOByOUid(Long oid, Long uid) {
        List<OrderVO> list=orderDao.findVOByOUid(oid,uid);

        return list;
    }

    @Override
    public OrderVO findVOByOUPid(Long oid, Long pid) {
        OrderVO order=orderDao.findVOByOUPid(oid,pid);

        return order;
    }

    @Override
    public List<PayVO> paySearch(Long pid) {
        List<PayVO> list=orderDao.paySearch(pid);

        return list;
    }

    @Override
    public void updatePStatus(Long oid, Long pid, Integer pStatus) {
        Integer rows=orderDao.updatePStatus(oid,pid,pStatus);
//        if(rows!=1){
  //          System.out.println("货物状态异常！");
    //    }
    }

    @Override
    public void updatePriority(Long oid, Long pid, Integer num) {
        Integer rows=orderDao.updatePriority(oid,pid,num);
//        if(rows!=1){
  //          System.out.println("货物状态异常！");
    //    }
    }

    @Override
    public void updateSubPriority(Long oid, Long pid, Integer num) {
        Integer rows=orderDao.updateSubPriority(oid,pid,num);

        //if(rows!=1){
          //  System.out.println("货物状态异常！");
        //}
    }

    @Override
    public List<PayVO> businessOrder(Long businessId) {
        List<PayVO> list=orderDao.businessOrder(businessId);

        return list;
    }
}
